<template>
  <div class="table_continer">
    <div style="position: absolute;top: -50px;right: 0px">
      <right-toolbar @queryTable="updateFlag" :columns="columns" />
    </div>
    <div v-if="tableflag">
      <!-- 列表 ------------------------------------------------------------------------------------------------------->
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="equipmentName" label="序号" width="100">
          <template slot-scope="scope">
            <div>{{ scope.$index + 1 }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[0].visible" prop="supplierNum" label="编号" min-width="170">
          <template slot-scope="scope">
            <el-link type="primary" @click="handledetailed(scope.row.equipmentRepairId)">{{ scope.row.equipmentRepairNum
            || '-'
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[1].visible" prop="eaPartInfoVo.equipmentNum" label="设备编号" min-width="120" />
        <el-table-column v-if="columns[2].visible" prop="eaPartInfoVo.equipmentName" label="设备名称" min-width="120"
          show-overflow-tooltip />
        <el-table-column v-if="columns[3].visible" prop="abnormalProjectName" label="异常项目" min-width="150"
          show-overflow-tooltip />
        <!-- <el-table-column v-if="columns[4].visible" prop="createBy" label="创建人" min-width="120" /> -->
        <el-table-column v-if="columns[4].visible" prop="status" label="状态" min-width="120">
          <template slot-scope="scope">
            <div :style="'color:' + ['#E6A23C', '#67C23A', '#F56C6C'][scope.row.status]">
              <i :class="['el-icon-warning', 'el-icon-success', 'el-icon-remove'][scope.row.status]" />
              <span>
                {{ ['未处理', '已处理', '已消除'][scope.row.status] }}
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[5].visible" label="消除时间" min-width="170">
          <template slot-scope="{row}">
            <div>{{ row.removeTime || '-' }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[6].visible" prop="describe" label="描述" min-width="170" show-overflow-tooltip>
          <template slot-scope="{row}">
            <div>{{ row.describe || '-' }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[7].visible" prop="remark" label="关联巡检编号" min-width="170">
          <template slot-scope="{row}">
            <div>{{ row.patrolNum || '-' }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[8].visible" prop="remark" label="关联维修工单" min-width="170">
          <template slot-scope="{row}">
            <div>{{ row.repairNum || '-' }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[9].visible" label="创建人" align="center" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.createBy || '-' }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns[10].visible" label="创建部门" align="center" min-width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.deptName || '-' }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns[11].visible" prop="createTime" label="创建时间" min-width="150" />
        <el-table-column fixed="right" label="操作" min-width="220">
          <template slot-scope="{row}">
            <!-- 此处ID字段特别注意 ------------------------------------------------------------------------------------>
            <el-button :disabled="row.status != 0" type="primary" v-hasPermi="['equipmentAssembly_left_eliminate']"
              size="small" icon="el-icon-bell" @click="handleEdit(row.equipmentRepairId)">已消除
            </el-button>
            <el-button :disabled="row.status != 0" type="warning" v-hasPermi="['equipmentAssembly_left_toMaintenance']"
              icon="el-icon-first-aid-kit" size="small" @click="handleAlter(row.equipmentRepairId)">转维修单</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- ------------------------------------------------------------------------------------------------------------>
    </div>
    <!-- --------------------------------分页器---------------------------------------------------------- -->
    <div class="table_continer_pagination">
      <el-pagination background :current-page="pageParams.pageNum" :page-sizes="[10, 20, 50, 100]"
        :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
/** 拉取列表远程方法 ---------------------------------------------------------------------------------------------------------- */
import { list } from '@/api/maintenance/management/equipmentAssembly.js'
import { getcolumnslist, Updatecolumns } from '@/api/menu.js'
/** ------------------------------------------------------------------------------------------------------------------------- */
import { pageCount } from '@/utils/pagination'
/** 查询参数处理 ---------------------------------------------------------------------------------------------------------- */
function processQueryParams (queryParams, pageParams) {
  const params = Object.assign({}, queryParams, pageParams)
  return params
}
/** ------------------------------------------------------------------------------------------------------------------------- */

export default {
  name: 'PaginationTable',
  data () {
    return {
      columns: [
        { key: 0, label: `编号`, visible: true, disabled: true },
        { key: 1, label: `设备编号`, visible: true },
        { key: 2, label: `设备名称`, visible: true },
        { key: 3, label: `异常项目`, visible: true },
        { key: 4, label: `状态`, visible: true },
        { key: 5, label: `消除时间`, visible: true, },
        { key: 6, label: `描述`, visible: true, },
        { key: 7, label: `关联巡检编号`, visible: true, },
        { key: 8, label: `关联维修工单`, visible: true },
        { key: 9, label: `创建人`, visible: true },
        { key: 10, label: `创建部门`, visible: true },
        { key: 11, label: `创建时间`, visible: true, },
      ],
      pagination: {
        total: 1,
        pageCount: 1
      },
      pageParams: {
        pageNum: 1,
        pageSize: 10,
        /** 列表默认排序字段 ---------------------------------------------------------------------------------------------------------- */
        orderByColumn: 'equipmentRepairId',
        /** ------------------------------------------------------------------------------------------------------------------------- */
        isAsc: 'desc'
      },
      queryParams: null,
      tableData: [],
      tableflag: true

    }
  },
  mounted () {
    this.refreshList();
    this.getcolumns();
  },
  watch: {
    columns: {//监听动态列修改调用更新方法
      handler (val) {
        this.tableflag = false
        this.updateFlag()
      },
      deep: true
    }
  },
  methods: {
    /**更新动态列数据 */
    async updateFlag () {
      let params = {
        viewKey: 'equipment_repair',
        columnFlag: JSON.stringify(this.columns)
      }
      await Updatecolumns(params)
      this.tableflag = true
      // this.getcolumns()
    },
    /**查询动态列数据 */
    async getcolumns () {
      let { data: data } = await getcolumnslist({ 'viewKey': 'equipment_repair' })
      console.log(data);
      let str = ''
      if (data.length !== 0) {
        data.map(item => {
          if (item.viewKey === 'equipment_repair') {
            // str = JSON.parse(eval('(' + item.columnFlag + ')'))
            str = JSON.parse(item.columnFlag)
            return false
          }
        })
        this.columns = str
      }
    },
    refreshList () {
      const params = processQueryParams(this.queryParams, this.pageParams)
      console.log('查询参数', params);
      // --------consolelog('查询参数', params)
      list(params).then((res) => {
        console.log('获取列表', res);
        // --------consolelog('获取列表', res)
        this.pagination.total = res.total
        this.pagination.pageCount = pageCount(this.pagination.total, this.pageParams.pageSize)
        this.tableData = res.data
      })
    },
    handleQuery (queryParams) {
      this.queryParams = queryParams
      this.refreshList()
    },
    handleSizeChange (value) {
      this.pageParams.pageSize = value
      this.refreshList()
    },
    handleCurrentChange (value) {
      this.pageParams.pageNum = value
      this.refreshList()
    },
    // 点击编号跳转列表详情-----------------------------
    handledetailed (id) {
      this.$emit('click_create_reception', id)
    },
    /** 传递点击【已消除】事件 ---------------------------------------------------------------------------------------------------------- */
    handleEdit (id) {
      this.$emit('click_edit', id)
    },
    // 传递点击【转维修单】 事件------------------------------------------------------------------------------------------------------------------
    handleAlter (id) {
      this.$emit('click_alter', id)
    }
  }
}
</script>
<style lang="scss" scoped>
.table_continer {
  position: relative;

  &_pagination {
    background-color: white;
    padding: 10px;
    margin-bottom: 10px;
  }
}

.table_left {
  display: flex;
  justify-content: left;
}
</style>
